//
//  CustomTableView.m
//  iLearning
//
//  Created by Sidney on 13-7-29.
//  Copyright (c) 2013年 iSoftstone infomation Technology (Group) Co.,Ltd. All rights reserved.
//
#import "CustomTableView.h"
//#import "DoYouLoveCell.h"
//#import "DoYouLoveModel.h"
#import "NearTableViewCell.h"
#import "ShoppingModel.h"
#import "ShopperModel.h"
#import "ShopperCell.h"

#import "ActivityCallupListCell.h"
#import "ActivityCallupModel.h"
#import "RepairHistoryListCell.h"
@interface CustomTableView()
{
    UIRefreshControl * refreshControl;
}


@end

@implementation CustomTableView
@synthesize tableViewDelegate,tableViewDataSource,selectedRowDataArr,tableType;

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self initWith];
        
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame type:(TABLE_VIEW_TYPE)type
{
    tableType = type;
    self = [super initWithFrame:frame];
    if (self) {
        [self initWith];
    }
    return self;
}

- (void)initWith
{
    self.delegate = self;
    self.dataSource = self;
    
    //[self hideExtralCellLine];
    self.backgroundColor = COLOR_RGBA(240, 240, 240, 1);
    tableViewDataSource  = [[NSMutableArray alloc] init];
    selectedRowDataArr   = [[NSMutableArray alloc] init];
    
    
}

- (void)hideExtralCellLine
{
    UIView * view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [self setTableFooterView:view];
    [self setTableHeaderView:view];
}

- (void)setTableViewDataSource:(NSArray *)data
{
    if (data) {
        [tableViewDataSource addObjectsFromArray:data];
        [self reloadData];
    }
}

- (void)setType:(TABLE_VIEW_TYPE)type
{
    tableType = type;
}

- (void)clearAllTableViewData
{
    [tableViewDataSource removeAllObjects];
    [selectedRowDataArr removeAllObjects];
    [self setContentOffset:CGPointMake(0, 0)];
}

- (void)setCellSelectedAtRow:(int)row section:(int)section
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    UITableViewCell * cell = [self cellForRowAtIndexPath:indexPath];// [[self visibleCells] objectAtIndex:row];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    //    [self selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}

- (void)addBtnPressedBlockMethod:(void (^)(CustomTableView * view,NSDictionary * selectedDt)) block
{
    self.btnPressedBlock = [block copy];
}

- (void)btnPressed:(UIButton *)btn
{
    
    NSDictionary * dic = [self.tableViewDataSource objectAtIndex:btn
                          .tag];
    self.btnPressedBlock(self,dic);
    
}

#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableViewDataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    int row = indexPath.row;
    int section = indexPath.section;
    
    switch (tableType) {
        case TAB_ITEM_TYPE_DOYOULOVELIST:{
            ShopperCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if(cell == nil)
            {
                cell = [[ShopperCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
//            ShopperModel* doYouLoveModel = [[ShopperModel alloc] initWithShopperInfo:[self.tableViewDataSource objectAtIndex:row]];
//            //[cell setShoppingModel:doYouLoveModel];
            
            cell.shopperModel = self.tableViewDataSource[indexPath.row];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }break;
        case TABLE_VIEW_TYPE_ACTIVITYCALLUPLIST:{
            ActivityCallupListCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if(cell == nil)
            {
                cell = [[ActivityCallupListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            ActivityCallupModel* activityCallup = [[ActivityCallupModel alloc] initWithActivityCallupInfo:[self.tableViewDataSource objectAtIndex:row]];
            [cell setModel:activityCallup];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }break;
        
        case TABLE_VIEW_TYPE_REPAIRHISTORYLIST:{
            RepairHistoryListCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if(cell == nil)
            {
                cell = [[RepairHistoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            [cell setRepairModel:[self.tableViewDataSource objectAtIndex:row]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }break;
        case TABLE_VIEW_TYPE_COMPLAINTLIST:{
            RepairHistoryListCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if(cell == nil)
            {
                cell = [[RepairHistoryListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            [cell setComplaintModel:[self.tableViewDataSource objectAtIndex:row]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }break;
        case TABLE_VIEW_TYPE_DEAFAULT:
        {
      
        }
        break;
        
    }
    return nil;
}

- (void)receiveBtnPressed:(UIButton *)sender
{
    int tag = sender.tag;
    if (self.btnPressedBlock) {
        self.btnPressedBlock(self,[self.tableViewDataSource objectAtIndex:tag]);
    }
}

#pragma mark TableviewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = indexPath.row;
    int section = indexPath.section;
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (tableType) {
        
        default:
        {
            if ([self.tableViewDelegate respondsToSelector:@selector(tableViewSelectedWithCellData:indexPath:tableView:)]) {
                [self.tableViewDelegate tableViewSelectedWithCellData:[tableViewDataSource objectAtIndex:row] indexPath:indexPath tableView:self];
            }
        }
        break;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    switch (tableType) {
        case TAB_ITEM_TYPE_DOYOULOVELIST:
        return 20;
        break;
        default:
        break;
    }
    return 0;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    switch (tableType) {
        case TAB_ITEM_TYPE_DOYOULOVELIST:
        {
            UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
            view.backgroundColor = COLOR_RGB(228, 229, 228);
            
            UIView *upLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 0.3)];
            upLine.backgroundColor =MAIN_GRAY_COLOR;
            [view addSubview:upLine];
            
            UIView *downLine = [[UIView alloc]initWithFrame:CGRectMake(0, 20-0.3, kWidth, 0.3)];
            downLine.backgroundColor =MAIN_GRAY_COLOR;
            [view addSubview:downLine];
            
            UILabel* label = [self createLabelWithFrame:CGRectMake(10, 1, kWidth, 18) text:@"猜你喜欢" font:14 textAli:NSTextAlignmentLeft textColor:MAIN_GRAY_COLOR];
//            label.backgroundColor = COLOR_RGB(228, 229, 228);
            [view addSubview:label];
            return view;
        }
        break;
        
        default:
        break;
    }
    return nil;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (tableType){
        case TABLE_VIEW_TYPE_CAR_LIST:
        return 55;
        break;
        case TAB_ITEM_TYPE_DOYOULOVELIST:
        return 99;
        break;
        case TABLE_VIEW_TYPE_ACTIVITYCALLUPLIST:{
            return 80;
        }break;
        case TABLE_VIEW_TYPE_REPAIRHISTORYLIST:{
            return 110;
        }break;
        case TABLE_VIEW_TYPE_COMPLAINTLIST:{
            return 110;
        }break;
        default:
        return 50;
        break;
    }
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (tableType) {
        
        default:
        return NO;
        break;
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = indexPath.row;
    if (editingStyle == UITableViewCellEditingStyleDelete){
        switch (tableType) {
            
            break;
            default:
            
            break;
        }
        
    }
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (tableType) {
        
        default:
        return nil;
        break;
    }
}

//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
//{
//    if ([self.tableViewDelegate respondsToSelector:@selector(tableViewWillBeginDragging:)]) {
//        [self.tableViewDelegate tableViewWillBeginDragging:self];
//    }
//    
//}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    [super scrollViewDidScroll:scrollView];
//    
//    CGFloat contentHeight = scrollView.contentSize.height;
//    CGFloat offsetY = scrollView.contentOffset.y;
//    CGFloat distanceFromBottom = contentHeight - offsetY;
//    static float min_offset = 400;
//    static float max_offset = 500;
//    //    NSLog(@"distanceFromBottom:%.2f",distanceFromBottom);
//    if (contentHeight != 0 && (min_offset < distanceFromBottom) &&( distanceFromBottom < max_offset) && distanceFromBottom > 0 && offsetY > 0) {
//        NSLog(@"auto load more data");
//        
//        if (self.scrollDir == DOWN_RIGHT) {
//            NSLog(@"DOWN_RIGHT");
//        }else{
//            NSLog(@"UP_LEFT");
//        }
//        
//        if (self.tableType == TABLE_VIEW_TYPE_CAR_LIST) {
//            if ([self.opetationDelegate respondsToSelector:@selector(getMoreData:)]) {
//                [self.opetationDelegate getMoreData:self];
//            }
//        }
//        
//    }
//}


-(UILabel*)createLabelWithFrame:(CGRect)frame text:(NSString*)text font:(int)font textAli:(NSTextAlignment)alig textColor:(UIColor*)tc{
    if(text == nil)
    text = @"";
    UILabel* label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = alig;
    label.textColor = tc;
    label.backgroundColor = [UIColor clearColor];
    return label;
}

@end
